<?php

namespace Biz\service\springHomeDecActivity;

use Biz\pockets\constInterface\SpringHomeDecConfig;
use Doraemon\pockets\config\IniConfig;
use Doraemon\tools\Presto\PrestoClientV2;

class ActivityUserGroupService implements SpringHomeDecConfig
{

    const HSQL_USER_GROUP_UNLOTTERY = "select t1.uid from
(select distinct cast(uid as int) as uid from oss.hhz.dwd_nginx_log
where day>=20220321 and day<=20220322
and url='/brand/home-decoration-festival/start'
and uid is not null and uid<>'') t1
left join
oss.dim.dim_user t2
on t1.uid=t2.uid
left join
(select distinct cast(uid as int) as uid from oss.hhz.dwd_nginx_log
where day>=20220321 and day<=20220322
and url='/f/t/api/SpringHomeDecActivity/Lottery') t3
on t1.uid=t3.uid
where t2.ban_push='允许推送' and t3.uid is null  and t1.uid > %d and t1.uid %% %d = %d
order by t1.uid limit %d";

    const HSQL_USER_GROUP_UNJION = "select cast(t1.uid as int) as uid from
(select distinct uid from oss.dwd.dwd_user_access_di
where day>=20220317 and day<=20220323) t1
left join
(select distinct uid  from oss.hhz.dwd_nginx_log
where day>=20220321
and url='/brand/home-decoration-festival/start') t2
on cast(t1.uid as varchar)=t2.uid
where t2.uid is null and t1.uid > %d and t1.uid %% %d = %d 
order by t1.uid limit %d";

    const HSQL_USER_GROUP_UNLOTTERY_TWO = "select t1.uid from
(select distinct cast(uid as int) as uid from oss.hhz.dwd_nginx_log
where day>=20220321 and day<=20220325
and url='/brand/home-decoration-festival/start'
and uid is not null and uid<>'') t1
left join
oss.dim.dim_user t2
on t1.uid=t2.uid
left join
(select distinct cast(uid as int) as uid from oss.hhz.dwd_nginx_log
where day>=20220324 and day<=20220325
and url='/f/t/api/SpringHomeDecActivity/Lottery') t3
on t1.uid=t3.uid
where t2.ban_push='允许推送' and t3.uid is null and t1.uid > %d and t1.uid %% %d = %d 
order by t1.uid limit %d";

    const HSQL_USER_GROUP_LUCKY_TICKET = "select uid from
m_hhz_activity.brand_activity.spring_home_dec_wish
where brick_count>=50 and uid > %d and uid %% %d = %d 
order by uid limit %d";

    const SCENE_SQLS = [
        //2022-03-23 未抽奖用户
        self::USER_GROUP_UNLOTTERY => self::HSQL_USER_GROUP_UNLOTTERY,
        //2022-03-24 至今未参与活动
        self::USER_GROUP_UNJION => self::HSQL_USER_GROUP_UNJION,
        //2022-03-26 未抽奖用户
        self::USER_GROUP_UNLOTTERY_TWO => self::HSQL_USER_GROUP_UNLOTTERY_TWO,
        //2022-03-29 锦鲤券用户
        self::USER_GROUP_LUCKY_TICKET => self::HSQL_USER_GROUP_LUCKY_TICKET,
    ];


    public static function getUids(int $scene, $params)
    {
        $sql = self::getSql($scene, $params);
        if (!$sql) {
            return false;
        }
        var_dump($sql);
        $list = [];
        $res = self::metabaseSqlExec($sql);
        if ($res && is_array($res)) {
            $list = $res;
            return $list;
        }
        return false;
    }


    private static function getSql(int $scene, array $params = [])
    {
        if (!isset(self::SCENE_SQLS[$scene])) {
            return false;
        }
        $sqlTpl = self::SCENE_SQLS[$scene];
        if (empty($sqlTpl)) {
            return false;
        }
        $sql = sprintf($sqlTpl, ...$params);

        return $sql;
    }


    private static function metabaseSqlExec(string $sql)
    {
        $prestoClient = self::getPrestoClient();
        if (!$prestoClient instanceof PrestoClientV2) {
            return false;
        }
        $prestoClient->Query($sql);
        $prestoClient->WaitQueryExec();
        $list = $prestoClient->GetData();
        if ($list && is_array($list)) {
            return $list;
        }
        return false;
    }


    private static function getPrestoClient()
    {
        $config = IniConfig::getConfigSelect('presto', 'brand');
        $presto = new PrestoClientV2("{$config['host']}:{$config['port']}/{$config['path']}", "hive",
            $config["username"], $config["password"]);
        return $presto;
    }


}